<!-- 页面右边字母导航条 -->
<template>
    <ul class="list">
        <li class="item" v-for="item of letters" :key="item" :ref="item" @click="handleLetterClick"
            @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd">
            {{ item }}
        </li>
    </ul>
</template>

<script>
export default {
    nama: 'CityAlphabet',
    props: {
        cities: Object
    },
    computed: {
        letters() {
            const letters = []
            for (let i in this.cities) {
                letters.push(i)
            }
            return letters
        }
    },
    data() {
        return {
            touchStatus: false,
            startY: 0,/* 字母表第一个字母距离该块顶部的距离 */
            timer: null
        }
    },
    updated() {
        this.startY = this.$refs['A'][0].offsetTop
    },
    methods: {
        handleLetterClick(e) {
            this.$emit('change', e.target.innerText)
        },
        handleTouchStart() {
            this.touchStatus = true
        },
        handleTouchMove(e) {
            if (this.touchStatus) {
                if (this.timer) {
                    clearTimeout(this.timer)
                }
                /* 降低函数调用的频率，当这个函数调用16后才能再次调用 */
                this.timer = setTimeout(() => {
                    const touchY = e.touches[0].clientY - 89
                    const index = Math.floor((touchY - this.startY) / 20)
                    if (index >= 0 && index < this.letters.length) {
                        this.$emit('change', this.letters[index])
                    }
                },16)

            }
        },
        handleTouchEnd() {
            this.touchStatus = false
        }
    }
}
</script>

<style lang="stylus" scoped>
    @import '../../../assets/styles/varibles.styl'
    .list
        display: flex
        flex-direction: column
        justify-content: center
        position absolute
        top:1.68rem
        bottom: 0
        width .4rem
        right: 0
        // background: red
        .item 
            line-height: .4rem
            text-align: center
            color: $bgColor
</style>